package com.meitianhui.convenitenbuy.module.wallet.presenter;

import com.google.gson.Gson;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.base.BaseResponse;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.http.ResponseObserver;
import com.meitianhui.convenitenbuy.main.http.MainApiDataSource;
import com.meitianhui.convenitenbuy.main.model.bean.UserToken;
import com.meitianhui.convenitenbuy.module.mine.model.UserAsset;
import com.meitianhui.convenitenbuy.module.wallet.contract.WalletContract;
import com.meitianhui.convenitenbuy.module.wallet.http.WalletApiDataSource;
import com.meitianhui.convenitenbuy.module.wallet.model.UserPropertyBean;
import com.meitianhui.convenitenbuy.module.wallet.ui.WalletFragment;
import com.meitianhui.convenitenbuy.util.PreferenceUtils;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by Administrator on 2017/11/2.
 */

public class WalletPresenter implements WalletContract.Presenter {

    private WalletFragment context;
    private final WalletContract.View view;

    public WalletPresenter(WalletFragment view, WalletFragment context) {
        this.view = view;
        this.context = context;
    }

    @Override
    public void attachView(Object view) {
    }

    @Override
    public void detachView() {
    }

    @Override
    public void getUserAsset() {
        Map<String, String> map = new HashMap<>();
        UserToken userToken = InfoProvider.getUserToken(App.getInstance());
        map.put("member_id", userToken.getMember_id());
        String json = new Gson().toJson(map);

        MainApiDataSource.getInstance()
                .getUserAsset(InfoProvider.getUserToken(App.getInstance()).getUser_token(),
                        "finance.memberAssetFind", json)
                .subscribe(new ResponseObserver<BaseResponse<UserAsset>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse<UserAsset> response) {
                        view.getUserAssetSuccess(response.data);
                        PreferenceUtils.saveObject(
                                App.getInstance(), Constants.USER_ASSET, response.data);
                    }
                });
    }

    @Override
    public void getUserProperty() {
        WalletApiDataSource.getInstance()
                .getUserProperty(InfoProvider.getUserToken(App.getInstance()).getUser_token())
                .subscribe(new ResponseObserver<BaseResponse<UserPropertyBean>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse<UserPropertyBean> response) {
                        view.getUserPropertySuccess(response.data);
                    }
                });
    }

    @Override
    public void update() {
    }

}
